<template>
  <div>
    <template>
      <Card style="width:850px;margin: auto;">
        <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="100"
              style="width: 300px;margin: auto;">

          <!--<FormItem label="产品代码" prop="productName" >-->
          <!--<Select v-model="formLeft.productName" filterable>-->
          <!--<Option v-for="item in productList" :value="item.name">{{ item.name }}</Option>-->
          <!--</Select>-->
          <!--</FormItem>-->

          <FormItem label="订单号:" prop="workOrderNo">
            <Input v-model="formLeft.workOrderNo" :maxlength="20" disabled></Input>
          </FormItem>

          <FormItem label="产品名称:" prop="productName">
            <Input v-model="formLeft.productName" :maxlength="20" disabled></Input>
          </FormItem>

          <FormItem label="计划数量:" prop="planQuantity">
            <InputNumber v-model="formLeft.planQuantity" :maxlength="10" style="width: 200px"/>
          </FormItem>

           <!-- {{RawMaterial }}-->
          <Select v-model="material" style="width: 210px" filterable>
          <Option v-for="item in list" :value="item.code" :key="item.code">{{ item.name }}</Option>
          </Select>

          <row type="flex" justify="center">
            <i-col span="20">

            </i-col>
            <i-col span="30">
              <Button type="primary" @click="OrderInsert">添加</Button>
              <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
            </i-col>
          </row>

        </Form>
      </Card>
    </template>
  </div>
</template>

<script>
  import {OrderInsert, ProductFindAll, BomFindAll, RawMaterialFindAll, SemiProductsFindAll} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 100000,//pageSize:每页几条数据
        total: 0,
        productList: [],
        data: [],
        RawMaterial:[],
        SemiProducts:[],
        list:[],
        material:'',
        formLeft: {
          productName: '',
          workOrderNo: '',
          planQuantity: 0,
          startTime: '',
          endTime: '',
          priority: ''
        },
        validateLibForm: {
          productName: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          workOrderNo: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          quantity: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          startTime: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          endTime: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      const orderNoFlag = this.$route.query.orderNo;
      const productNameNoFlag = this.$route.query.productName;
      this.formLeft.workOrderNo = orderNoFlag;
      this.formLeft.productName = productNameNoFlag;

      /* if(orderNoFlag){
         this.formLeft.orderNo = orderNoFlag;
         this.OrderFindByOrderNo();
         this.ProductFindAll();
       }*/
      this.BomFindAll();
     
    },
    methods: {
      dateplanDate(value, key) {
        this.formLeft[key] = value
      },
      datedeliverDate(value, key) {
        this.formLeft[key] = value
      },
      OrderInsert() {
        if (this.formLeft.productName !== null && this.formLeft.productName !== "") {
          if (this.formLeft.orderNo !== null && this.formLeft.orderNo !== "") {
            if (this.formLeft.planQuantity !== null && this.formLeft.planQuantity !== "") {
              if (this.formLeft.startTime !== null && this.formLeft.startTime !== "") {
                if (this.formLeft.endTime !== null && this.formLeft.endTime !== "") {
                  OrderInsert(this.formLeft).then(res => {
                    this.$Message.success("添加成功");
                    this.$router.go(-1)
                  })
                } else {
                  this.$Message.error("交货日期不能为空");
                }
              } else {
                this.$Message.error("计划日期不能为空");
              }
            } else {
              this.$Message.error("数量不能为空");
            }
          } else {
            this.$Message.error("订单号不能为空");
          }
        } else {
          this.$Message.error("产品代码不能为空");
        }
      },
      BomFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
          productName: this.formLeft.productName
        };
        BomFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.data = data.data.data.list;
            this.total = data.data.data.total;
           // console.log(this.data)
            for (let i = 0; i < this.data.length; i++) {
              if (this.data[i].materialType !== '半成品') {
                this.RawMaterialFindAll(this.data[i].materielNo);
              } else {
                this.SemiProductsFindAll((this.data[i].materielNo))
              }
            }
          }
        })
      },
      //原材料
      RawMaterialFindAll(materielNo) {
        var data = {
          page: this.page,
          pageSize: 10000,
          materialType: materielNo
        };
        RawMaterialFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.list = data.data.data.list;
           /* this.RawMaterial.push(this.list);
            console.log(this.RawMaterial)*/
          }
        })
      },
      //半成品
      SemiProductsFindAll(materielNo) {
        var data = {
          page: this.page,
          pageSize: 10000,
          materialType: materielNo
        };
        SemiProductsFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.SemiProducts = data.data.data.list;
          //  console.log(this.SemiProducts)
          }
        })
      },
      ProductFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
        };
        ProductFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.productList = data.data.data.list;
          }
        })
      },
      back() {
        this.$router.go(-1);
      },

    }
  }
</script>


